﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSem40701.Admin
{
    public partial class AnswerAdd : System.Web.UI.Page
    {
        /// <summary>
        /// Declare member variables here 
        /// </summary>

        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.cbbQuestion.DataBind();
                FillQuestion();
            }
        }
        /// <summary>
        /// Get selected question
        /// </summary>
        private void FillQuestion()
        {
            if (Request.QueryString["qid"].ToString() != String.Empty)
            {
                foreach (ListItem item in this.cbbQuestion.Items)
                {
                    if (item.Value == Request.QueryString["qid"].ToString())
                    {
                        item.Selected = true;
                    }
                }
            }
        }
        /// <summary>
        /// Add answer
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter dpAnswer =
                    new AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter();
                int ok = dpAnswer.tbl_Answer_Insert(
                    Convert.ToInt32(this.cbbQuestion.SelectedValue.ToString()),
                    Server.HtmlEncode(this.txtAnswerContent.Value.Trim()), 
                    this.chkbCorrect.Checked, 
                    this.txtAnswerTitle.Text.Trim(), 
                    Convert.ToInt32(this.cbbAnswerStatus.SelectedValue.ToString()));
                if (ok > 0)
                {
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    Show_Dialog(FAIL);
                }
            }
            catch
            { 
                //bao loi
                Show_Dialog(FAIL);
            }
        }

        /// <summary>
        /// Thông báo thành công hay thất bại ở đây
        /// </summary>
        /// <param name="Check"></param>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Không thành công";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnAdd_ModalPopupExtender.Enabled = true;
            this.btnAdd_ModalPopupExtender.Show();
        }
        /// <summary>
        /// Xay ra khi nguoi dung an nut Close
        /// </summary>
        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        /// <summary>
        /// Xay ra khi nguoi dung an nut Yes
        /// </summary>
        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        /// <summary>
        /// Close dialog
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("QuestionEdit.aspx?qid=" + Request.QueryString["qid"].ToString());
                    break;
                case FAIL:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
                default:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
    }
}
